package com.midongtech.cpd.utils;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.JsonObject;
import com.midongtech.cpd.bean.AppData;
import com.midongtech.cpd.constants.AccountConstant;
import com.midongtech.cpd.constants.UrlConstant;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class HttpManager {

   private static final String TAG = "HttpManager";

   public static void uploadApplist(Context context,CommonCallBack callBack) {
      JsonObject jsonObject = new JsonObject ();
      jsonObject.addProperty("imei",DeviceUtil.getDeviceId(context));
      jsonObject.addProperty("oaid",DeviceUtil.getOaid(context));
      jsonObject.addProperty("cuid", AccountConstant.CUID);
      jsonObject.addProperty("pkgs",AppUtils.getAppList(context));

      String p = CpdEncryptUtil.encryptAndEncode(jsonObject.toString());
      Map<String, String> paramsMap = new HashMap<>();
      paramsMap.put("appids",AccountConstant.APPID);
      paramsMap.put("p",p);
      HttpUtil.httpPost(UrlConstant.URL_FPKG,paramsMap,callBack);

   }

   public static void getAds(Context context,CommonCallBack callBack) {
      JsonObject jsonObject = new JsonObject ();
      jsonObject.addProperty("imei",DeviceUtil.getDeviceId(context));
      jsonObject.addProperty("sip",DeviceUtil.getIp(context));
      jsonObject.addProperty("mac",DeviceUtil.getMac(context));
      jsonObject.addProperty("vendor",DeviceUtil.getVendor());
      jsonObject.addProperty("model",DeviceUtil.getModel());
      jsonObject.addProperty("oaid",DeviceUtil.getOaid(context));
      jsonObject.addProperty("androidId",DeviceUtil.getAndroidId(context));
      jsonObject.addProperty("userAgent",DeviceUtil.getUserAgent(context));
      jsonObject.addProperty("osVersion",DeviceUtil.getOsVersion());
      jsonObject.addProperty("isroot",DeviceUtil.isRoot());
      jsonObject.addProperty("cuid", AccountConstant.CUID);
      jsonObject.addProperty("apiLevel",DeviceUtil.getApiLevel());

      String p = CpdEncryptUtil.encryptAndEncode(jsonObject.toString());
      Map<String, String> paramsMap = new HashMap<>();
      paramsMap.put("appids",AccountConstant.APPID);
      paramsMap.put("p",p);
      HttpUtil.httpPost(UrlConstant.URL_GETADS,paramsMap,callBack);

   }

   public static void monitor(Context context, AppData data,int status, CommonCallBack callBack) {
      if (data == null) {
         return;
      }
      Log.i(TAG,"monitor:"+status);
      JsonObject jsonObject = new JsonObject ();
      jsonObject.addProperty("imei",DeviceUtil.getDeviceId(context));
      jsonObject.addProperty("sip",DeviceUtil.getIp(context));
      jsonObject.addProperty("mac",DeviceUtil.getMac(context));
      jsonObject.addProperty("vendor",DeviceUtil.getVendor());
      jsonObject.addProperty("model",DeviceUtil.getModel());
      jsonObject.addProperty("oaid",DeviceUtil.getOaid(context));
      jsonObject.addProperty("androidId",DeviceUtil.getAndroidId(context));
      jsonObject.addProperty("userAgent",DeviceUtil.getUserAgent(context));
      jsonObject.addProperty("osVersion",DeviceUtil.getOsVersion());
      jsonObject.addProperty("isroot",DeviceUtil.isRoot());
      jsonObject.addProperty("cuid", AccountConstant.CUID);
      jsonObject.addProperty("apiLevel",DeviceUtil.getApiLevel());

      jsonObject.addProperty("source",data.getSource());
      jsonObject.addProperty("apkid",data.getApkid());
      jsonObject.addProperty("is_qiandao",data.getIs_qiandao());
      jsonObject.addProperty("pkg",data.getPkg());
      jsonObject.addProperty("status",status);



      String p = CpdEncryptUtil.encryptAndEncode(jsonObject.toString());
      Map<String, String> paramsMap = new HashMap<>();
      paramsMap.put("appids",AccountConstant.APPID);
      paramsMap.put("p",p);
      HttpUtil.httpPost(UrlConstant.URL_MONITOR,paramsMap,callBack);

   }

}
